package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.AProduct;
import com.ruoyi.system.domain.G_Collection;
import com.ruoyi.system.domain.vo.CollectionVo;
import com.ruoyi.system.service.IAProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ICollectionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 收藏Controller
 * 
 * @author 张颖
 * @date 2025-01-08
 */
@Controller
@RequestMapping("/system/collection")
public class CollectionController extends BaseController
{
    private String prefix = "system/collection";

    @Autowired
    private ICollectionService collectionService;

    @Resource
    private IAProductService productService;

    @RequiresPermissions("system:collection:view")
    @GetMapping()
    public String collection()
    {
        return prefix + "/collection";
    }

    /**
     * 查询收藏列表
     */
    @RequiresPermissions("system:collection:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(G_Collection collection)
    {
        startPage();
        List<G_Collection> list = collectionService.selectCollectionList(collection);
        List<CollectionVo> collectionVos = new ArrayList<>();
        list.forEach(ct -> {
            CollectionVo collectionVo = new CollectionVo();
            BeanUtils.copyProperties(ct,collectionVo);
            collectionVo.setUserName(ShiroUtils.getSysUser().getUserName());
            AProduct product = productService.selectAProductById(ct.getGoodsId());
            collectionVo.setGoodsName(Optional.ofNullable(product).map(AProduct::getProductName).orElse("商品源已被删除"));
            collectionVos.add(collectionVo);
        });
        return getDataTable(collectionVos);
    }

    /**
     * 导出收藏列表
     */
    @RequiresPermissions("system:collection:export")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(G_Collection collection)
    {
        List<G_Collection> list = collectionService.selectCollectionList(collection);
        ExcelUtil<G_Collection> util = new ExcelUtil<G_Collection>(G_Collection.class);
        return util.exportExcel(list, "收藏数据");
    }

    /**
     * 新增收藏
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("productList", productService.selectAProductList(new AProduct()));
        return prefix + "/add";
    }

    /**
     * 新增保存收藏
     */
    @RequiresPermissions("system:collection:add")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(G_Collection collection)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        collection.setUserId(sysUser.getUserId());
        return toAjax(collectionService.insertCollection(collection));
    }

    /**
     * 修改收藏
     */
    @RequiresPermissions("system:collection:edit")
    @GetMapping("/edit/{collectionId}")
    public String edit(@PathVariable("collectionId") Long collectionId, ModelMap mmap)
    {
        G_Collection collection = collectionService.selectCollectionByCollectionId(collectionId);
        mmap.put("collection", collection);
        mmap.put("productList", productService.selectAProductList(new AProduct()));
        return prefix + "/edit";
    }

    /**
     * 修改保存收藏
     */
    @RequiresPermissions("system:collection:edit")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(G_Collection collection)
    {
        return toAjax(collectionService.updateCollection(collection));
    }

    /**
     * 删除收藏
     */
    @RequiresPermissions("system:collection:remove")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(collectionService.softDeleteCollectionByCollectionIds(ids));
    }
}
